/**
 * 通用返回结果
 */
export class Result<T> {
  /**
   * 状态码: 0:成功, 1:失败
   */
  code: number;
  /**
   * 返回信息
   */
  msg: string;
  /**
   * 返回数据
   */
  data: T;

  static successData<T>(data: T): Result<T> {
    const result: Result<T> = new Result();
    result.code = 0;
    result.msg = '成功';
    result.data = data;
    return result;
  }

  static success<T>(): Result<T> {
    return this.successData(null);
  }

  static failByMsg<T>(msg: string): Result<T> {
    const result: Result<T> = new Result();
    result.code = -1;
    result.msg = msg;
    return result;
  }

  static failByCodeMsg<T>(code: number, msg: string): Result<T> {
    const result: Result<T> = new Result();
    result.code = code;
    result.msg = msg;
    return result;
  }

  static failByCode<T>(code: number): Result<T> {
    const result: Result<T> = this.fail();
    result.code = code;
    return result;
  }

  static fail<T>(): Result<T> {
    return this.failByMsg('失败');
  }
}
